<?php

/*
 * This class is used for as Model for Quotation table 
 */

class Model_Quotation extends Model_Base {

    protected $_name = 'tbl_quotation';
    protected $_primaryKey = 'qn_id';

    /**
     * Fetch an individual entry
     *
     * @param  int|string $id
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('quotation' => 'tbl_quotation'))
                ->where('qn_id = ?', $id);
        // see reasoning in fetchEntries() as to why we return only an array
        $quotationData = $this->fetchRow($select);


        if (!empty($quotationData)) {
            $quotation = $quotationData->toArray();

            $qn_id = $quotation['qn_id'];

            //Load Quotation Reference Model
            $quotationReferenceModel = new Model_Quotationreference();
            $quotationReference = $quotationReferenceModel->fetchEntryById($qn_id);

            //Load Quotation Image Model
            $quotationImageModel = new Model_Quotationimages();
            $quotationImage = $quotationImageModel->fetchEntryById($qn_id);

            return Array(
                'QUOTATION' => $quotation,
                'QUOTATION_REFERENCE' => $quotationReference,
                'QUOTATION_IMAGES' => $quotationImage,
            );
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get quotations
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchQuotations($where = "", $limit = "", $offset = "0", $order = 'qn_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('quotation' => 'tbl_quotation'));
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        $select->order($order . " " . $sort);
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get total quotation
     * @return int
     */
    public function getQuotationCount($where) {
        $select = $this->select()
                ->from(array('quotation' => 'tbl_quotation'), Array('cnt' => 'count(*)'));
        if ($where != '') {
            $select->where($where);
        }
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }

    /**
     *  This function is used to delete quotations
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function deleteQuotation($quotationtArray) {
        if (count($quotationtArray) <= 0) {
            return;
        }
        foreach ($quotationtArray as $_quotation) {
            $config = Zend_Registry::get('config');
            $quotationData = $this->fetchEntryById($_quotation);
            $quotation = $quotationData['QUOTATION'];
            
            //Delete Quotation table entry
            $this->delete('qn_id = ' . (int) $quotation['qn_id']);
            
            //Load Quotation referece table 
            $quotationReferenceModel = new Model_Quotationreference();
            //Delete Quotation Reference table entry
            $quotationReferenceModel->delete('qn_id = ' . (int) $quotation['qn_id']);
            
            //Load Quotation Image table 
            $quotationImageModel = new Model_Quotationimages();
            
            //Delete Quotation Image table entry
            $quotationImageModel->delete('qn_id = ' . (int) $quotation['qn_id']);
            
            //Delete the Images
            $quotationImages = $quotationData['QUOTATION_IMAGES'];
            $folder = $config->UPLOAD_PATH . "quotation/";
            foreach($quotationImages as $_images) {
                    //Deletion of the images
                    $image = $folder . $_images['qni_image'];
                    $thumb_image = $folder . "thumb_" . $_images['qni_image'];
                    if (file_exists($image)) {
                        @unlink($image);
                    }
                    if (file_exists($thumb_image)) {
                        @unlink($thumb_image);
                    }
            }
            
            
        }
    }

}
